# from app.db import db
from app.db import BaseDocument
import mongoengine as db
import datetime


class Role(BaseDocument):
    """[summary]
    "_id": {
            "$oid": "6082a4ed1f9fe4001b2f3ac3"
        },
        "isDefaultRole": False,
        "name": "管理员",
        "permissions": [
            {
                "selfOnly": False,
                "_id": {
                        "$oid": "6082a4ed1f9fe4001b2f3ac4"
                },
                "module": "auth",
                "permissionId": "listUser"
            }
        ]
    Args:
        BaseDocument ([type]): [description]

    Returns:
        [type]: [description]
    """
    isDefaultRole = db.BooleanField(default=False)
    name = db.StringField()
    permissions = db.ListField()

    meta = {
        'collection': 'roles',
        'strict': False
    }


class User(BaseDocument):

    """
     "type": "cas",
     "roleIds": [
          {
               "$oid": "604d75ce261b3b0011a1a4cb"
          }
     ],
     "phoneVerified": True,
     "emailVerified": False,
     "identified": True,
     "enableSingleDevice": False,
     "enableMfa": False,
     "accessLimit": -1,
     "username": "admin@knownsec.com",
     "realname": "xxx",
     "attribution": "SC",
     "email": "admin@knownsec.com",
     "phone": "18780106577",
     "address": "test",
     "idCardNum": "5108221**********32",
     "registerTime": {
          "$date": "2016-03-09T02:39:42.943Z"
     },
     "qq": "123232",
     "createAt": {
          "$date": "2021-03-14T02:47:28.221Z"
     },
     "lastActiveTime": {
          "$date": "2021-05-06T06:37:27.014Z"
     },
     "__v": 0,
     "balanceId": {
          "$oid": "604d7940405c990011467f3a"
     },
     "planId": {
          "$oid": "604d75cc261b3b0011a1a496"
     },
     "wechat": {},
     "disclaimerAgreeTime": {
          "$date": "2021-03-14T08:00:58.270Z"
     }


    Args:
        db ([type]): [description]

    Returns:
        [type]: [description]
    """

    type = db.StringField(default='builtin')
    roleIds = db.ListField(db.ReferenceField(Role))
    phoneVerified = db.BooleanField(default=False)
    emailVerified = db.BooleanField(default=False)
    identified = db.BooleanField(default=False)
    enableSingleDevice = db.BooleanField(default=False)
    enableMfa = db.BooleanField(default=False)
    accessLimit = db.IntField(default=-1)
    username = db.StringField(required=True)
    realname = db.StringField()
    attribution = db.StringField()
    email = db.EmailField()
    phone = db.StringField()
    address = db.StringField()
    idCardNum = db.StringField()
    registerTime = db.DateTimeField()
    qq = db.StringField()
    createAt = db.DateTimeField()
    lastActiveTime = db.DateTimeField()
    balanceId = db.ObjectIdField()
    planId = db.ObjectIdField()
    wechat = db.DictField()
    disclaimerAgreeTime = db.DateTimeField()

    meta = {
        'collection': 'users',
        'strict': False
    }

    def save(self, *args, **kwargs):
        if not self.createAt:
            self.createAt = datetime.datetime.utcnow()
        return super(User, self).save(*args, **kwargs)


class Token(BaseDocument):
    """
     "roleIds": [
          {
               "$oid": "604d75ce261b3b0011a1a49f"
          }
     ],
     "note": "运营后台",
     "expireAt": {
          "$date": "2027-10-14T00:00:00.000Z"
     },
     "createBy": {
          "$oid": "604d75ce261b3b0011a1a49e"
     },
     "createAt": {
          "$date": "2021-03-14T02:35:11.674Z"
     },
     "token": ".eysmS13A",
     "__v": 0


    Args:
        db ([type]): [description]

    Returns:
        [type]: [description]
    """

    roleIds = db.ListField(db.ReferenceField(Role))
    note = db.StringField()
    expireAt = db.DateTimeField()
    createBy = db.ObjectIdField()
    createAt = db.DateTimeField()
    token = db.StringField()

    meta = {
        'collection': 'tokens',
        'strict': False
    }

    def save(self, *args, **kwargs):
        if not self.createAt:
            self.createAt = datetime.datetime.utcnow()
        return super(Token, self).save(*args, **kwargs)
